package com.skyworth.web.waiter;

import com.skyworth.web.admin.menu.domain.MenuModel;
import com.skyworth.web.admin.menu.service.MenuService;
import com.skyworth.web.admin.table.service.ITableService;
import com.skyworth.web.admin.user.domain.UserModel;
import com.skyworth.web.admin.user.service.UserService;
import com.skyworth.web.common.ApiResponse;
import com.skyworth.web.order.domain.OrderModel;
import com.skyworth.web.order.form.OrderForm;
import com.skyworth.web.order.service.OrderService;
import com.skyworth.web.wechat.common.MD5Encode;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

import static com.skyworth.web.order.service.OrderService.ORDER_CONFIRMED;

/**
 * Created by cody on 2016/9/19.
 *
 * 服务员端相关RESTful接口
 *
 */
@Controller
@RequestMapping("/api/waiter")
public class WaiterRestController {

    @Autowired
    OrderService orderService;

    @Autowired
    ITableService tableService;

    @Autowired
    UserService userService;

    @Autowired
    MenuService menuService;


    //menu

    /**
     *
     * @param keyword
     * @param apiResponse
     * @return
     */
    @ResponseBody
    @RequestMapping(value = {"/menu/search/{storeId}/{keyword}"},method = RequestMethod.GET)
    public Object searchMenu(@PathVariable Long storeId,@PathVariable String keyword,
                             ApiResponse apiResponse){
        try{
            List<MenuModel> list = menuService.queryMenu(storeId,keyword);
            apiResponse.setSuccess(true);
            apiResponse.setData(list);
        }catch (Exception e){
            e.printStackTrace();
            apiResponse.setSuccess(false);
        }
        return apiResponse;
    }

    /**
     *服务员退出登录
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping(value = {"/logout"},method = RequestMethod.POST)
    public Object logout(HttpServletRequest request,HttpServletResponse response){
        ApiResponse apiResponse = new ApiResponse();

        Subject subject = SecurityUtils.getSubject();
        if(subject != null){
            subject.logout();
        }
        apiResponse.setSuccess(true);
        WaiterCookie.removeCookie(response);

        return apiResponse;
    }


    /**
     * 服务员登录接口
     * @param user
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping(value={"/login"},method = RequestMethod.POST)
    public Object login(@RequestBody Map<String,String> user, HttpServletRequest request, HttpServletResponse response){
        ApiResponse apiResponse = new ApiResponse();

        String username = user.get("username");
        String password = user.get("password");

        Subject subject = SecurityUtils.getSubject();
        String md5Password = MD5Encode.encode(password);

        UsernamePasswordToken token = new UsernamePasswordToken(username,md5Password);
        try{

            subject.login(token);
            if(subject.isAuthenticated()){
                UserModel userModel = userService.findUserByUsername(username);
                WaiterCookie.writeSuccesCookie(username,userModel,response);
                apiResponse.setSuccess(true);
                apiResponse.setMessage("登录成功");
            }else{
                apiResponse.setSuccess(false);
                apiResponse.setMessage("用户名或者密码错误");
                WaiterCookie.writeErrorCookie(response);
            }

        }catch (Exception e){
            e.printStackTrace();
        }

        return apiResponse;
    }


    /**
     * 获取正在进行的订单
     * @param storeId
     * @return
     */
    @ResponseBody
    @RequestMapping(value = {"/orders/ongoing/{storeId}"})
    public Object onGoingOrders(@PathVariable Long storeId){
        ApiResponse apiResponse = new ApiResponse();
        try {
            List<OrderModel> list = orderService.getOnGoingOrders(storeId);
            apiResponse.setSuccess(true);
            apiResponse.setData(list);
        }catch (Exception e){
            apiResponse.setSuccess(false);
            e.printStackTrace();
        }

        return apiResponse;
    }

    /**
     * 服务员端，退菜
     * @param itemId ---- order 与 menu 映射表的id
     * @param apiResponse
     * @return
     */
    @ResponseBody
    @RequestMapping(value={"/order/modify/rmitem/{itemId}"})
    public Object deleteOrderItem(@PathVariable Long itemId,ApiResponse apiResponse){

        OrderModel orderModel = orderService.removeMenuItem(itemId);

        apiResponse.setSuccess(true);
        apiResponse.setData(orderModel);

        return apiResponse;
    }

    /**
     * 服务员端添加一个订单的接口
     */
    @ResponseBody
    @RequestMapping(value={"/order/create"},method = RequestMethod.POST)
    public Object createOrder(@ModelAttribute OrderForm orderForm){

        OrderModel om= OrderForm.toOrderModel(orderForm);
        om.state = ORDER_CONFIRMED;

        List<Map<String,Object>> foods = orderForm.getFoods();

        ApiResponse apiResponse = new ApiResponse();

        try{
            Object result = orderService.waiterCreateOrder(om,foods);
            apiResponse.setSuccess(true);
            apiResponse.setData(result);
        }catch (Exception e){
            apiResponse.setSuccess(false);
        }
        return apiResponse;
    }







}
